(function() {
    var app = angular.module("chinaums.study", []);
    app.controller('picUploadCtrl', function ($scope, $http) {
        $scope.allRepos = [];
        $scope.checkRepo = function(repos, id) {
            var i = repos.indexOf(id);
            if (i >= 0) {
                delete repos[i];
            } else {
                repos.push(id);
            }
        };
        $scope.submit = function(formData) {
            var params = new FormData();
            params.append("pic", formData.pic);
            params.append("repos", formData.repos);
            $http.post("pic/retrieval", params, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined},
                withCredentials: false
            }).then(function(response) {
                $scope.result = response.data;
                $scope.errorMessage = "";
            }, function(error, status, headers, config) {
                $scope.errorMessage = error;
            });
        };
        $http.get("pic/load-repos").then(function (response) {
            $scope.allRepos = response.data;
        }, function(error, status, headers, config) {
            $scope.errorMessage = error;
        });
    });
    app.directive("fileModel", function($parse) {
        return function(scope, elements, attrs) {
            var model = $parse(attrs.fileModel);
            elements.on('change', function() {
                model.assign(scope, elements[0].files[0]);
            });
        };
    });
})();